#pragma once

#include <iostream>
#include <sys/epoll.h>

// 封装一个epoll类
class Epoll
{
    const static int max_fds = 128; // 最大fd个数
    const static int timeout = 5000; // 等待间隔
public:
    Epoll(int timeout = timeout): _timeout(timeout)
    {}

    ~Epoll(){}

    void EpollCreate()
    {
        _epfd = epoll_create(max_fds);
        if(_epfd < 0) // 负数创建失败
            exit(5);
    }

    bool AddSockToEpoll(int sock, uint32_t events)
    {
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = sock;
        return epoll_ctl(_epfd, EPOLL_CTL_ADD, sock, &ev) == 0;
    }

    bool DelSockFromEpoll(int sock)
    {
        return epoll_ctl(_epfd, EPOLL_CTL_DEL, sock, NULL) == 0;
    }

    int EpollWait(struct epoll_event _evs_array[], int num)
    {
        return epoll_wait(_epfd, _evs_array, num, _timeout);
    }

    bool EpollCtl(int sock, uint32_t events)
    {
        events |= EPOLLET;
        struct epoll_event ev;
        ev.events = events;
        ev.data.fd = sock;
        return epoll_ctl(_epfd, EPOLL_CTL_MOD, sock, &ev);
    }

private:
    int _epfd;
    int _timeout;
};